package com.dc.controller;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.dc.comments.AjaxResponse;
import com.dc.entity.ProductType;
import com.dc.service.ProductTypeService;
import com.dc.service.impl.ProductTypeServiceImpl;
import com.dc.tools.PageData;
import com.google.gson.Gson;

/**
 * Servlet implementation class ProductTypeServlet
 */
@WebServlet("/ProductTypeServlet")
public class ProductTypeServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
	
	//实例ProductTypeService
	ProductTypeService pts = new ProductTypeServiceImpl();
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public ProductTypeServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		//设置编码
		request.setCharacterEncoding("utf-8");
		String op ="";
		//接收op
		if(request.getParameter("op") != null) {
			op = request.getParameter("op");
		}
		
		//根据op判断执行哪个方法
		if("queryByPage".equals(op)) {
			doQueryByPage(request, response);
		}else if("add".equals(op)) {
			doAdd(request, response);
		}else if("ajaxrequest".equals(op)) {
			doQueryAjax(response);
		}else if("del".equals(op)) {
			doDelete(request, response, op);
		}else if("update".equals(op)) {
			doUpdate(request, response);
		}
		
	}

	private void doUpdate(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		//接收参数
		String typeidstring = request.getParameter("updatetypeid");
		String typename = request.getParameter("updatetypename");
		String typeinfo = request.getParameter("updatettypeinfo");
		System.out.println("typeid:"+typeidstring+",typename:"+typename+",typeinfo:"+typeinfo);
		//给typeid转int类型
		int typeid = 0;
		if(request.getParameter("updatetypeid") != null) {
			typeid = Integer.parseInt(request.getParameter("updatetypeid"));
		}
		//创建ProductType对象
		ProductType producttype = new ProductType(typeid, typename, typeinfo);
		//调用更新的方法
		boolean f = pts.updateProductType(producttype);
		System.out.println(f);
		if(f) {
			request.setAttribute("msg", "修改成功！");
			//成功查询显示出来
			doQueryByPage(request, response);
		}else {
			request.setAttribute("msg", "修改失败！请重新修改。");
		}
	}

	private void doDelete(HttpServletRequest request, HttpServletResponse response, String op) throws IOException {
		//设置响应头编码
		response.setContentType("application/json;charset=utf-8");
		System.out.println("op:"+op);
		int typeid = 0;
		
		String q = request.getParameter("typeid");
		System.out.println("typeid:"+q);
		//获取typeid
		if(request.getParameter("typeid") != null) {
			typeid = Integer.parseInt(request.getParameter("typeid"));
		}
		//调用service中的删除方法
		boolean f =pts.deleteProductType(typeid);
		
		System.out.println("删除成功了吗:"+f);
		//创建AjaxResponse对象
		AjaxResponse res = new AjaxResponse(f+"",200);
		//创建Gson对象
		Gson gson = new Gson();
		String jsonString = gson.toJson(res);
		//响应到jsp
		PrintWriter out = response.getWriter();
		out.print(jsonString);
		//关闭流
		out.close();
	}

	private void doQueryAjax(HttpServletResponse response) throws IOException {
		//设置响应编码
		response.setContentType("application/json;charset=utf-8");
		//调用service中的查询方法
		List<ProductType> list = pts.selectProductType();
		//创建Gson对象
		Gson gson = new Gson();
		//转换为json字符串
		String jsonString = gson.toJson(list);
		//得到PrintWriter对象
		PrintWriter out = response.getWriter();
		//把jsonString响应到页面
		out.print(jsonString);
		//关闭流
		out.close();
	}

	private void doAdd(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		//接收参数
		String typeidstring = request.getParameter("typeid");
		String typename = request.getParameter("typename");
		String typeinfo = request.getParameter("typeinfo");
		System.out.println("typeid:"+typeidstring+",typename:"+typename+",typeinfo:"+typeinfo);
		//给typeid转int类型
		int typeid = 0;
		if(request.getParameter("typeid") != null) {
			typeid = Integer.parseInt(request.getParameter("typeid"));
		}
		//创建ProductType对象
		ProductType producttype = new ProductType(typename, typeinfo);
		//调用增加方法
		boolean f = pts.addProductType(producttype);
		System.out.println(f);
		if(f) {
			request.setAttribute("msg", "增加成功！");
			//成功查询显示出来
			doQueryByPage(request, response);
		}else {
			request.setAttribute("msg", "增加失败！请重新添加。");
		}
	}

	private void doQueryByPage(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		String keywords = "";
		//接收keywords
		if(request.getParameter("keywords") != null) {
			keywords = request.getParameter("keywords");
		}
		System.out.println("********************");
		System.out.println(keywords);
		int page = 1;
		int pageSize = 10;
		//获取当前页和每页展示数据
		if(request.getParameter("page") != null) {
			page = Integer.parseInt(request.getParameter("page"));
		}
		if(request.getParameter("pageSize") != null) {
			pageSize = Integer.parseInt(request.getParameter("pageSize"));
		}
		//调用service中的显示方法
		PageData<ProductType> pd = pts.selectProductTypeByPage(page, pageSize, keywords);
		//将搜索的关键字转发到JSP
		request.setAttribute("keywords", keywords);
		//将pd传递到jsp
		request.setAttribute("pd", pd);
		//转发
		request.getRequestDispatcher("producttype.jsp").forward(request, response);
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		doGet(request, response);
	}

}
